<template>
  <div class="product-add">
    <el-form size="" ref="form" label-width="100px">
      <el-form-item label="积分兑换商品"></el-form-item>
      <el-form-item size="" label="">
        <el-col>
          <div class="common-level-rail">
            <el-button size="" icon="Plus" @click="addClick"
              >添加商品</el-button
            >
          </div>
        </el-col>
        <el-col>
          <div class="pb50">
            <el-table size="" :data="tableData" border>
              <el-table-column label="图片" width="60">
                <template #default="scope">
                  <img :width="30" v-img-url="scope.row.product_image" />
                </template>
              </el-table-column>
              <el-table-column
                prop="product_name"
                label="商品"
              ></el-table-column>
              <el-table-column prop="point_price" label="商品价格">
                <template #default="scope"
                  ><el-input v-model="scope.row.point_price" size="small">
                    <template #suffix>元</template>
                  </el-input>
                </template>
              </el-table-column>
              <!-- <el-table-column prop="quota" label="限购">
                <template #default="scope">
                  <el-input v-model="scope.row.quota" size="small"
                    ><template #suffix>件</template></el-input
                  >
                </template>
              </el-table-column> -->
              <el-table-column fixed="right" label="操作" width="90">
                <template #default="scope">
                  <el-button
                    @click="deleteClick(scope.row)"
                    link
                    type="primary"
                    size=""
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-col>
      </el-form-item>
      <!--提交-->
      <div class="common-button-wrapper">
        <el-button type="primary" size="" @click="onSubmit" :loading="loading"
          >提交</el-button
        >
      </div>
    </el-form>
    <!--选择商品-->
    <Product
      :isproduct="isproduct"
      @closeDialog="closeProductDialogFunc($event)"
      >产品列表弹出层</Product
    >
  </div>
</template>

<script>
import { getPointProduct, savePointProduct } from "@/api/pointproduct.js";
import Product from "@/components/product/Product.vue";
export default {
  components: {
    /*商品选择*/
    Product,
  },
  data() {
    return {
      tableData: [],
      isproduct: false,
      product_arr: [],
      Data: [],
      loading: false,
    };
  },
  created() {
    /*获取数据*/
    this.getData();
  },
  methods: {
    /*获取当前数据*/
    getData() {
      let self = this;
      self.loading = true;
      console.log(this.grade_id);
      getPointProduct()
        .then((data) => {
          self.loading = false;
          self.tableData = data.data;
        })
        .catch((error) => {
          self.loading = false;
        });
    },

    /**
     * 保存数据
     */
    onSubmit() {
      let products = [];
      for (let i = 0; i < this.tableData.length; i++) {
        products.push({
          product_id: this.tableData[i].product_id,
          point_price: this.tableData[i].point_price,
          quota: this.tableData[i].quota,
        });
      }
      let param = {
        products: products,
      };
      savePointProduct(param).then((res) => {
        ElMessage({
          message: "保存成功",
          type: "success",
        });
      });
    },
    /*关闭弹窗*/
    closeProductDialogFunc(e) {
      this.isproduct = e.openDialog;
      if (e.type == "success") {
        if (this.product_arr.indexOf(e.params.product_id) > -1) {
          ElMessage({
            message: "你已选中该产品",
            type: "error",
          });
          return false;
        }
        let param = {};
        param.product_id = e.params.product_id;
        param.product_name = e.params.product_name;
        param.product_image = e.params.product_image;
        param.point_price = e.params.product_price;
        param.quota = 0;
        this.tableData.push(param);
        this.product_arr.push(e.params.product_id);
      }
    },
    /*添加商品*/
    addClick() {
      console.log("here");
      this.isproduct = true;
    },
    /*删除商品*/
    deleteClick(e) {
      this.tableData = this.tableData.filter((item) => {
        return item.product_id != e.product_id;
      });
    },
  },
};
</script>

<style></style>
